<template>
  <div id="infoList">
    <div>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-circle-plus"
        @click="toAdd"
        >新增</el-button
      >
      <el-button
        size="small"
        type="danger"
        @click="handleDelete(null)"
        :disabled="multipleSelection.length == 0"
        >批量删除</el-button
      >
    </div>
    <div>
      <el-table
        border
        stripe
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="索引" type="index" width="55"></el-table-column>
        <!-- <el-table-column width="55" type="index" label="编号"> </el-table-column> -->
        <el-table-column label="序号">
          <template slot-scope="scope">
            <!-- <i class="el-icon-time"></i> -->
            <span style="margin-left: 10px">{{ scope.row.no }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名">
          <template slot-scope="scope">
            <el-tag type="warning" size="medium">{{ scope.row.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="简介" width="180">
          <template slot-scope="scope">
            <i class="el-icon-chat-line-square"></i>
            <!-- <div class="button"> -->
            {{ scope.row.description }}
            <!-- </div> -->
          </template>
        </el-table-column>
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      :title="title"
      :visible.sync="visible"
      :before-close="beforeClose"
    >
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item prop="name" label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="description"
          label="介绍"
          :label-width="formLabelWidth"
        >
          <el-input
            type="textarea"
            v-model="form.description"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="no" label="序号" :label-width="formLabelWidth">
          <el-input v-model.number="form.no" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toCancel">取 消</el-button>
        <el-button type="primary" @click="toSave('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "../../utils/request";
export default {
  data() {
    return {
      filtersNo: [],
      multipleSelection: [],
      tableData: [],
      // 模态框可见性 dialog  modal
      visible: false,
      // 模态框的数据
      form: {},
      // 模态框表单控件标题的宽度
      formLabelWidth: "120px",
      // 模态框的标题
      title: "录入分类信息",
      // 表单校验规则
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        description: [
          { required: true, message: "请输入介绍", trigger: "blur" },
        ],
        no: [
          { required: true, message: "请输入序号", trigger: "blur" },
          { type: "number", message: "年龄必须为数字值" },
        ],
      },
    };
  },
  computed: {},
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return "warning-row";
      } else if (rowIndex % 2 === 0) {
        return "success-row";
      }
      return "";
    },
    // 多选
    handleSelectionChange(val) {
      // val是用户选中的数据对象数组
      // val = [ { "id": 3, "name": "最新进展", "description": "疫情的动态", "no": 1, "parentId": null }, { "id": 4, "name": "公告", "description": null, "no": 2, "parentId": null } ] ;
      // this.multipleSelection = [3,4]
      this.multipleSelection = val.map((item) => item.id);
    },
    // 批量删除
    // toBatchDelete() {
    //   console.log("批量删除");
    // },
    // 关闭模态框
    toCancel() {
      this.$refs["form"].resetFields();
      this.visible = false;
    },
    // 模态框关闭之前
    beforeClose(done) {
      this.$refs["form"].resetFields();
      done();
    },
    // 点击新增按钮
    toAdd() {
      this.form = {};
      this.visible = true;
      this.title = "录入分类信息";
    },
    // 点击确定按钮
    toSave(formName) {
      this.$refs[formName].validate((valid) => {
        // 校验通过
        if (valid) {
          // console.log(this.form, "=====");
          axios.post("/category/saveOrUpdate", this.form).then((res) => {
            if (res.status == 200) {
              //保存成功，关闭模态框，刷新数据
              this.$notify({
                title: "成功",
                message: "保存成功",
                type: "success",
              });
              this.toCancel();
              this.findAll();
            } else {
              this.$notify.error({
                title: "失败",
                message: "保存失败",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑分类信息";
      this.form = { ...row };
      this.visible = true;
    },
    // 删除
    handleDelete(id) {
      // id 就是要删除的分类的对象的id
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // await等待后面异步函数运行完毕，拿到响应的数据，然后返回
          let res;
          if (id) {
            res = await axios.get("/category/deleteById", { params: { id } });
          } else {
            res = await axios.post("/category/batchDelete", {
              ids: this.multipleSelection.toString(),
            });
          }
          this.$notify({
            title: res.status == 200 ? "成功" : "失败",
            message: res.statusText,
            type: res.status == 200 ? "success" : "error",
          });
          this.findAll();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    findAll() {
      axios.get("/category/findAll").then((res) => {
        this.tableData = res.data;
      });
    },
  },
  created() {
    this.findAll();
  },
};
</script>
<style scoped>
.button {
  margin-left: 10px;
  display: inline-block;
}
</style>